Skip to main content

update_translations.yaml

This is a GitHub Workflow added to a target project to enable updating translations in production

name: Update Translations

on:
repository_dispatch:
types: [update-translations]

jobs:
update-translations:
runs-on: ubuntu-latest

steps:
# ───────────────────────────────────────────────────────────────
# 1) Get requester information from token and verify collaborator status
# ───────────────────────────────────────────────────────────────
- name: Get requester information and verify collaborator status
id: get-user
env:
GITHUB_API: https://api.github.com
TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
USERNAME: "${{ github.event.client_payload.username }}"
run: |
# Check if we got a valid username
if [ -z "$USERNAME" ]; then
echo "❌ No username provided in client payload - aborting."
exit 1
fi

echo "Username: $USERNAME"
echo "username=$USERNAME" >> $GITHUB_OUTPUT
echo "name=$USERNAME" >> $GITHUB_OUTPUT

# Verify if user is a collaborator
status=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: token $TOKEN" \
$GITHUB_API/repos/$REPO/collaborators/$USERNAME)

if [ "$status" -eq 404 ]; then
echo "✋ $USERNAME is not a collaborator — aborting."
exit 1
fi

echo "✅ $USERNAME is a collaborator - proceeding."

# ───────────────────────────────────────────────────────────────
# 2) Checkout the main branch
# ───────────────────────────────────────────────────────────────
- name: Checkout main branch
uses: actions/checkout@v3
with:
ref: main

# ───────────────────────────────────────────────────────────────
# 3) Apply your JSON update (via here-document)
# ───────────────────────────────────────────────────────────────
- name: Update JSON file
env:
FILE_PATH: ${{ github.event.client_payload.file_path }}
CONTENT: ${{ github.event.client_payload.content }}
run: |
# Create directory if it doesn't exist
mkdir -p "$(dirname "$FILE_PATH")"

# Write the JSON content via here-doc so we never have to escape quotes/apostrophes/etc.
cat <<EOF > "$FILE_PATH"
$CONTENT
EOF

# Verify the file was created successfully
if [ -f "$FILE_PATH" ]; then
echo "✅ JSON file created/updated successfully"
cat "$FILE_PATH"
else
echo "❌ Failed to create/update JSON file" >&2
exit 1
fi

# ───────────────────────────────────────────────────────────────
# 4) Commit & push the translation changes directly to main
# ───────────────────────────────────────────────────────────────
- name: Commit & push changes
env:
FILE_PATH: ${{ github.event.client_payload.file_path }}
run: |
# Configure author for this repository (no --global)
git config user.name "${{ steps.get-user.outputs.name }}"
git config user.email "${{ steps.get-user.outputs.username }}@users.noreply.github.com"

# Stage file
git add "$FILE_PATH"

# Commit only if there are changes
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "${{ github.event.client_payload.commit_message }}"
# Push to main
git push origin HEAD:main
echo "✅ Successfully updated translations and pushed to main"
fi